define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'template'], function ($, undefined, Backend, Table, Form, Template) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'yungift/fund/user/index' + location.search,
                    del_url: 'yungift/fund/user/del' + location.search,
                    multi_url: 'yungift/fund/user/multi' + location.search,
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                columns: [
                    [
                        // 该列为复选框字段,如果后台的返回state值将会默认选中
                        {
                            field: 'state',
                            checkbox: true
                        },
                        // sortable为是否排序,operate为搜索时的操作符,visible表示是否可见
                        {
                            field: 'id',
                            title: '成员ID',
                            sortable: true,
                        },
                        {
                            field: 'group_id',
                            title: __('成员分组'),
                            searchList: Config.groupMap,
                            formatter: function (value, row, index) {
                                return row.group_text;
                            }
                        },
                        {
                            field: 'name',
                            title: __('姓名'),
                            operate: 'LIKE %...%',
                            placeholder: '模糊搜索',
                        },
                        {
                            field: 'number',
                            title: __('编号'),
                            operate: 'LIKE %...%',
                            placeholder: '模糊搜索',
                        },
                        {
                            field: 'phone',
                            title: __('手机号'),
                            operate: 'LIKE %...%',
                            placeholder: '模糊搜索',
                        },
                        {
                            field: 'state',
                            title: __('账户状态'),
                            searchList: Config.stateList,
                            formatter: function (value, row, index) {
                                if (!row.fid) {
                                    return '<a class="btn-change text-info" data-url="yungift/fund/user/handle" data-confirm="激活礼币账户？" data-id="' + row.id + '" data-action="init" data-params="aid=' + Config.activity_id + '">未激活</a>';
                                }
                                if (row.status === 1) {
                                    return '<a class="btn-change text-success" data-url="yungift/fund/user/handle" data-confirm="礼币账户设为屏蔽？" data-id="' + row.id + '" data-action="close" data-params="aid=' + Config.activity_id + '"><i class="fa fa-toggle-on fa-2x"></i></a>';
                                }
                                return '<a class="btn-change" data-url="yungift/fund/user/handle" data-confirm="礼币账户设为正常？" data-id="' + row.id + '" data-action="open" data-params="aid=' + Config.activity_id + '"><i class="fa fa-toggle-on fa-flip-horizontal text-gray fa-2x"></i></a>';
                            },
                        },
                        {
                            field: 'fund_user.id',
                            title: '账户ID',
                            sortable: true,
                            formatter: function (value, row, index) {
                                return row.fid;
                            }
                        },
                        {
                            field: 'fund_user.amount_give',
                            title: __('发放礼币'),
                            sortable: true,
                            operate: false,
                            formatter: function (value, row, index) {
                                return row.amount_give;
                            }
                        },
                        {
                            field: 'fund_user.amount_left',
                            title: __('剩余礼币'),
                            sortable: true,
                            operate: false,
                            formatter: function (value, row, index) {
                                return row.amount_left;
                            }
                        },
                        {
                            field: 'fund_user.amount_use',
                            title: __('花费礼币'),
                            sortable: true,
                            operate: false,
                            formatter: function (value, row, index) {
                                return row.amount_use;
                            }
                        },
                        {
                            field: 'fund_user.amount_back',
                            title: __('退回礼币'),
                            sortable: true,
                            operate: false,
                            formatter: function (value, row, index) {
                                return row.amount_back;
                            }
                        },
                        {
                            field: 'updated_at',
                            title: __('更新时间'),
                            sortable: true,
                            formatter: Table.api.formatter.datetime,
                            operate: false,
                        },
                        // 启用时间段搜索
                        {
                            field: 'fund_user.created_at',
                            title: __('新增时间'),
                            sortable: true,
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: function (value, row, index) {
                                return Table.api.formatter.datetime(row.created_at, row, index);
                            },
                        },
                        // 操作栏,默认有编辑、删除或排序按钮,可自定义配置buttons来扩展按钮
                        {
                            field: 'operate',
                            width: "150px",
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'record',
                                    text: __('记录'),
                                    title: function (row) {
                                        return __('礼币记录') + ' - ' + Config.activity.name + ' - ' + row.name;
                                    },
                                    classname: 'btn btn-xs btn-info btn-dialog',
                                    extend: 'data-area=\'["100%","100%"]\'',
                                    icon: 'fa fa-history',
                                    visible: function (row) {
                                        return row.fid;
                                    },
                                    url: function (row) {
                                        return 'yungift/fund/record/index?activity_id=' + Config.activity_id + '&member_id=' + row.member_id;
                                    }
                                },
                                {
                                    name: 'plus',
                                    title: __('增加礼币'),
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    icon: 'fa fa-plus',
                                    visible: function (row) {
                                        return row.fid;
                                    },
                                    url: function (row) {
                                        return 'yungift/fund/user/plus?activity_id=' + Config.activity_id + '&member_id=' + row.member_id;
                                    },
                                    extend: 'data-area=\'["250px","250px"]\'',
                                },
                                {
                                    name: 'minus',
                                    title: __('减少礼币'),
                                    classname: 'btn btn-xs btn-primary btn-dialog',
                                    visible: function (row) {
                                        return row.fid;
                                    },
                                    icon: 'fa fa-minus',
                                    url: function (row) {
                                        return 'yungift/fund/user/minus?activity_id=' + Config.activity_id + '&member_id=' + row.member_id;
                                    },
                                    extend: 'data-area=\'["250px","250px"]\'',
                                },
                                {
                                    name: 'order',
                                    text: __('订单'),
                                    title: function (row) {
                                        return __('相关订单') + ' - ' + row.name;
                                    },
                                    classname: 'btn btn-xs btn-info btn-dialog',
                                    visible: function (row) {
                                        return row.amount_use > 0;
                                    },
                                    extend: 'data-area=\'["100%","100%"]\'',
                                    icon: 'fa fa-list',
                                    url: function (row) {
                                        return 'yungift/gift/order/index?model_type=activity&model_account=' + row.id;
                                    },
                                },
                            ],
                            formatter: function (value, row, index) {
                                var that = $.extend({}, this);
                                var table = $(that.table).clone(true);
                                if (!row.fid) {
                                    $(table).data("operate-del", null);
                                }
                                that.table = table;
                                return Table.api.formatter.operate.call(that, value, row, index);
                            }
                        },
                    ],
                ],
                // 快捷搜索,这里可在控制器定义快捷搜索的字段
                search: false,
                // 启用普通表单搜索
                commonSearch: true,
                // 显示切换视图（table/card）按钮
                showToggle: false,
                // 显示导出按钮
                showExport: true,
                // 启用跨页选择
                maintainSelected: true,
                // 启用固定列
                fixedColumns: true,
                // 固定右侧列数
                fixedRightNumber: 1,
                // 可以控制是否默认显示搜索单表,false则隐藏,默认为false
                searchFormVisible: true,
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            // 自定义批量处理礼币开始 ***************************
            var createCode = function (ids, layero) {
                var options = table.bootstrapTable('getOptions');
                console.log('options', options);
                var search = options.queryParams({});
                console.log('search', search);
                $("input[name=search]", layero).val(options.searchText);
                $("input[name=ids]", layero).val(ids);
                $("input[name=filter]", layero).val(search.filter);
                $("input[name=op]", layero).val(search.op);
                Form.api.submit($("form", layero), function () {
                    table.bootstrapTable('refresh');
                });
            };
            $(document).on("click", ".btn-amount", function () {
                var ids = Table.api.selectedids(table);
                var page = table.bootstrapTable('getData');
                var all = table.bootstrapTable('getOptions').totalRows;
                console.log('choose', ids, page, all);
                Layer.confirm("如果礼币账户没有激活，会先激活再操作<form action='" + Fast.api.fixurl('yungift/fund/user/handleAmount' + location.search) + "' method='post'>" +
                    "<input type='hidden' name='ids'>" +
                    "<input type='hidden' name='filter'>" +
                    "<input type='hidden' name='op'>" +
                    "<input type='hidden' name='search'>" +
                    "<br>" +
                    "<div class='form-group'><input class='form-control' type='number' name='init_amount' placeholder='初始化礼币数量，只对发放礼币为0的有效'></div>" +
                    "<div class='form-group'><input class='form-control' type='number' name='change_amount' placeholder='增减礼币数量，大于0增加，小于0减少'></div>" +
                    "</form>", {
                    title: '批量处理礼币',
                    btn: ["选中项(" + ids.length + "条)", "本页(" + page.length + "条)", "全部(" + all + "条)"],
                    success: function (layero, index) {
                        // 弹窗显示后，统一3个按钮的样式
                        $(".layui-layer-btn a", layero).addClass("layui-layer-btn0");
                    },
                    yes: function (index, layero) {
                        createCode(ids.join(","), layero);
                        Layer.close(index);
                        return true;
                    },
                    btn2: function (index, layero) {
                        var ids = [];
                        $.each(page, function (i, j) {
                            ids.push(j.id);
                        });
                        createCode(ids.join(","), layero);
                        return true;
                    },
                    btn3: function (index, layero) {
                        createCode("all", layero);
                        return true;
                    }
                })
            });
            // 自定义批量处理礼币结束 ***************************
        },
        plus: function () {
            Controller.api.bindevent();
        },
        minus: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            formatter: {  // 渲染的方法
            },
            events: {  // 绑定事件的方法
            }
        }
    };
    return Controller;
});
